package com.jack.vipmis.vip.api.web.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jack.vipmis.common.web.controller.BaseController;
import com.jack.vipmis.vip.action.entity.ActionEntity;
import com.jack.vipmis.vip.action.service.ActionEntityService;
import com.jack.vipmis.vip.api.util.MsgUtil;

/**
 * 活动API
 * 
 * @author JackLiang
 * @version 1.0
 * @date 2014-10-9
 */
@Controller
@RequestMapping(value = "/api/action")
public final class ActionApiController extends BaseController<ActionEntity, Long> {

	@Autowired
	private ActionEntityService	actionEntityService;

	/**
	 * 获取Action信息根据ID
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "find")
	@ResponseBody
	public Map<String, Object> findActionById(@RequestParam("id") long id) {
		try {
			ActionEntity action = actionEntityService.findOne(id);
			if (action != null) {
				return MsgUtil.doneOk(action);
			}
		} catch (Exception e) {
		}
		return MsgUtil.doneFail();
	}

	/**
	 * 分页查询
	 * 
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "list")
	@ResponseBody
	public Map<String, Object> list(@RequestParam("page") int page, @RequestParam("pageSize") int pageSize) {
		try {
			Page<ActionEntity> list = actionEntityService.list(page, pageSize);
			if (!list.getContent().isEmpty()) {
				return MsgUtil.doneOk(list.getContent());
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return MsgUtil.doneFail();
	}

	/**
	 * 更新被要求人数
	 * 
	 * @param id
	 * @param number
	 * @return
	 */
	@RequestMapping(value = "update")
	@ResponseBody
	public Map<String, Object> updateInviter(@RequestParam("id") long id, @RequestParam("num") long number) {
		
		ActionEntity m = actionEntityService.updateInviters(id, number);
		if (m != null) {
			return MsgUtil.doneOk(m);
		}
		return MsgUtil.doneFail();
	}
}
